<?php
// 应用公共文件


use app\common\exception\BusinessException;
use app\common\model\UserVip;
use app\common\service\GlobalsVariableService;
use app\common\service\UserService;
use app\common\service\WxService;
use think\facade\Config;
use think\Request;

/**
 * getRandAvatar
 * @return string
 */
function getRandAvatar(): string
{
    $config = Config::get('mcp.default_avatar');
    $min = $config['file_name_min'];
    $max = $config['file_name_max'];
    $num = mt_rand($min, $max);
    $fileHost = Config::get('mcp.fileHost');
    return $fileHost . $config['path'] . $num . $config['postfix'];
}

/**
 * fillUserAvatar
 * @param string $avatar
 * @return string
 */
function fillUserAvatar(string $avatar): string
{
    if (strpos($avatar, 'http') !== false) {
        return $avatar;
    }
    $apiHost = Config::get('mcp.apiHost');
    return $apiHost . $avatar;
}

/**
 * fillUserSignature
 * @param bool $is_vip
 * @return string
 */
function fillUserVipAvatar(bool $is_vip): string
{
    $fileHost = Config::get('mcp.fileHost');
    $filePath = $fileHost . '/app_img/';
    return $is_vip ? $filePath . 'user_lv_2.png' : $filePath . 'user_lv_1.png';
}

/**
 * appIsAudit
 */
function appIsAudit(): bool
{
    // 检查客户端版本和服务端版本是否一致
    $nowAppVersion = GlobalsVariableService::get('appVersion');
    $appVersion = Config::get('mcp.appVersion');
    $standardAppIsAudit = $nowAppVersion === $appVersion;

    // 非微信小程序渠道跳过
    $appPlatform = GlobalsVariableService::get('appPlatform');
    if (!$appPlatform) {
        throw new BusinessException('未知平台');
    }
    if (!in_array($appPlatform, [20, 21, 40, 41])) {
        return false;
    }

    // 未登录匹配正常审核判断
    $userId = app(app\api\Request::class)->param('userId');
    if (!$userId) {
        return $standardAppIsAudit;
    }

    // 白名单跳过检查
    $userService = new UserService();
    $userInfo = $userService->getUserById($userId, '*');
    if ($userInfo && $userInfo['whitelist'] == 1) {
        return false;
    }

    // 其他全部匹配正常审核判断
    return $standardAppIsAudit;
}

/**
 * checkSafeByWxCode
 * @param $appId
 * @param $wxCode
 * @return string
 * @throws BusinessException
 */
function checkSafeByWxCode($appId, $wxCode): string
{
    $wx = new WxService();
    $wx->setAppId($appId);
    try {
        $wxInfo = $wx->getUserInfoByCode($wxCode);
        return $wxInfo['openid'] ?? '';
    } catch (Exception $exception) {
        throw new BusinessException('您的网络环境异常,请重试');
    }
}

/**
 * isIos
 * @return bool
 */
function isIos(): bool
{
    // 真实检测
    $http_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($http_user_agent, 'iphone') !== false) {
        return true;
    }
    if (strpos($http_user_agent, 'ipad') !== false) {
        return true;
    }
    if (strpos($http_user_agent, 'ipod') !== false) {
        return true;
    }
    return false;
}

/**
 * 是否小程序环境
 * @return bool
 */
function isWxMcp(): bool
{
    $appPlatform = GlobalsVariableService::get('appPlatform');
    if (in_array($appPlatform, [20, 21])) {
        return true;
    }
    return false;
}

/**
 * 是否小程序手机版
 * @return bool
 */
function isMobileWxMcp(): bool
{
    $appPlatform = GlobalsVariableService::get('appPlatform');
    return $appPlatform == 20;
}

/**
 * 是否小程序电脑版
 * @return bool
 */
function isPcWxMcp(): bool
{
    $appPlatform = GlobalsVariableService::get('appPlatform');
    return $appPlatform == 21;
}

/**
 * 是否H5
 * @return bool
 */
function isH5(): bool
{
    $appPlatform = GlobalsVariableService::get('appPlatform');
    return $appPlatform == 10 || $appPlatform == 11;
}

/**
 * 是否app
 * @return bool
 */
function isApp()
{
    $appPlatform = GlobalsVariableService::get('appPlatform');
    return $appPlatform == 30;
}

/**
 * 是否虚拟支付
 * @return bool
 */
function isIosVirtualPay(): bool
{
    // 是否IOS设备
    $isIos = isIos();
    if (!$isIos) {
        return false;
    }

    // IOS_APP非虚拟支付
    if (isApp()) {
        return false;
    }

    // IOS_H5非虚拟支付
    if (isH5()) {
        return false;
    }

    // 是否微信小程序PC终端
    $isMobileWxMcp = isPcWxMcp();
    if ($isMobileWxMcp) {
        return false;
    }

    // 用户属于白名单则正常支付
    $userId = app(app\api\Request::class)->param('userId', 0);
    if ($userId) {
        $isWhiteUser = app(UserService::class)->isWhiteUser($userId);
        if ($isWhiteUser) {
            return false;
        }
    }

    // 属于虚拟支付
    return true;
}

/**
 * getAuthorization
 * @return mixed|string
 */
function getAuthorization()
{
    $request = app(Request::class);
    $header = $request->header();
    $token = $header['token'] ?? '';
    if ($token) {
        return $token;
    }
    $authorization = $header['authorization'] ?? '';
    if ($authorization) {
        return $authorization;
    }
    return '';
}

/**
 * isVip
 * @return bool
 */
function isVip(): bool
{
    $userId = app(app\api\Request::class)->param('userId', 0);
    if (!$userId) {
        return false;
    }
    return app(UserVip::class)->checkIsVip($userId);
}
